package com.booktrade.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.booktrade.dto.CartDTO;
import com.booktrade.entity.Books;
import com.booktrade.entity.Cart;
import com.booktrade.result.Result;
import com.booktrade.service.IBooksService;
import com.booktrade.service.ICartService;
import com.booktrade.util.BaseContext;
import com.booktrade.vo.CartShopVo;
import com.booktrade.vo.CartVo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车表，存储用户购物车中添加的书籍信息 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {

    private final ICartService cartService;


    /**

     * 3)	显示购物车商品总价和优惠信息。TODO 应该根据商家id分组
     */

    @GetMapping("/list")
    @Operation(summary = "购物车列表")
    public Result<CartShopVo> list(){
        Integer userId = BaseContext.getCurrentId();

       CartShopVo cartShopVo = cartService.Cartlist(userId);
        return Result.success(cartShopVo);
    }



    /**
     * 添加购物车
     * @param cartDTO
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加购物车")
    public Result add(@RequestBody CartDTO cartDTO){
        Integer userId = BaseContext.getCurrentId();
       cartService.addCart(cartDTO, userId);
       return Result.success();
    }
    @PutMapping
    @Operation(summary = "删除购物车商品数量-1")
    public Result delete(@RequestBody CartDTO cartDTO){
        Integer userId = BaseContext.getCurrentId();
        cartService.deleteCart(cartDTO, userId);
        return Result.success();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除购物车商品")
    public Result deleteById(@RequestBody CartDTO cartDTO){
        Integer userId = BaseContext.getCurrentId();
        cartService.remove(new LambdaQueryWrapper<Cart>().eq(Cart::getBookId, cartDTO.getBookId()));
        return Result.success();
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空购物车")
    public Result clear(){
        Integer userId = BaseContext.getCurrentId();
        cartService.remove(new LambdaQueryWrapper<Cart>().eq(Cart::getUserId, userId));
        return Result.success();
    }





}
